package org.come.until;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.come.bean.LoginResult;
import org.come.bean.NpcMenuBean;
import org.come.bean.NpcSureMenuBean;
import org.come.bean.RoleInitBean;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class ControlNpcXmlUntil {
	private static NpcMenuBean npcmenubean;// 初始化类型
	// 依据type取出对应npc 菜单
	private static String type;
	// 增加的额外菜单
	private static NpcSureMenuBean sureBean = new NpcSureMenuBean();
	// 保存获取的节点信息
	private static String noteName1;
	// 取出对应的角色信息，依据specise_id
	private static String speciesId;
	// 执行条件的真假
	private static boolean yesOrNo = false;
	// 初始化角色initbean
	private static RoleInitBean roleInitBean;
	// 查出新建角色的loginResult
	private static LoginResult loginResult = new LoginResult();

	public ControlNpcXmlUntil() throws Exception {
		GetXmlPath(null);
	}

	/**
	 * 进入扫描器，传入路径
	 *
	 * @throws Exception
	 */
	public static void GetXmlPath(String path){
		try {
			// 创建SAXReader对象
			SAXReader reader = new SAXReader();
			Document document = reader.read(Util.class.getClassLoader().getResourceAsStream(path));
			// 获取根节点元素对象
			Element root = document.getRootElement();
			// 遍历
			listNodes(root, path);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

	// 遍历当前节点下的所有节点
	public static void listNodes(Element node, String path) {
		// 首先获取当前节点的所有属性节点
		List<Attribute> list = node.attributes();
		// 依据属性进行遍历
		// 遍历属性节点
		for (Attribute attribute : list) {
			if ("roleState.xml".equals(path)) {
				GetRoleInitXml(attribute.getName(), attribute.getValue());
			} else if ("npcMenu.xml".equals(path)) {
				NpcBean(attribute.getName(), attribute.getValue());
			}
		}

		// 同时迭代当前节点下面的所有子节点
		// 使用递归
		Iterator<Element> iterator = node.elementIterator();
		while (iterator.hasNext()) {
			Element e = iterator.next();
			listNodes(e, path);
		}
	}

	/**依据属性名称，放入不同的bean中的类*/
	public static void NpcBean(String note, String noteName) {
		// 进入初始化每个npc
		if ("type".equals(note) && noteName.equals(type)) {
			yesOrNo = true;
		} else if ("type".equals(note) && (!noteName.equals(type))) {
			yesOrNo = false;
		}
		if (yesOrNo) {
			switch (note) {
				case "type":
					type = noteName;
					npcmenubean.setType(type);
					break;
				case "RaceId":
					// 执行的条件
					sureBean.setConditions(noteName);
					npcmenubean.setSureBean(sureBean);
					break;
				case "sureinittitle":
					// 符合条件的菜单
					noteName1 = noteName;
					break;
				case "sureinitActionId":
					// 符合条件的actionId
					sureBean.getMenuAndMethod().put(noteName1, noteName);
					break;
				case "inittitle":
					// 不符合条件菜单
					noteName1 = noteName;
					break;
				case "initActionId":
					// 不符合条件的actionID
					npcmenubean.getInittitle().put(noteName1, noteName);
					break;
			}
		}

	}

	/**
	 * 初始化roleXml
	 */
	public static void GetRoleInitXml(String note, String noteName) {
		if ("sepciesId".equals(note) && (speciesId.equals(noteName))) {
			// 符合取出的要求
			yesOrNo = true;
			roleInitBean = new RoleInitBean();
		} else if ("sepciesId".equals(note) && (!speciesId.equals(noteName))) {
			yesOrNo = false;
		}
		if (yesOrNo) {

			switch (note) {
				case "sepciesId":
					// 设置种类id
					loginResult.setSpecies_id(new BigDecimal(noteName));
					break;
				case "raceId":
					// 设置种族id
					loginResult.setRace_id(new BigDecimal(noteName));
					break;
				case "raceName":
					// 设置种族名字
					loginResult.setRace_name(noteName);
					break;
				case "sex":
					// 设置性别
					loginResult.setSex(noteName);
					break;
				case "localName":
					// 设置初始话名字
					loginResult.setLocalname(noteName);
					break;
			}

		}

	}

	public static NpcMenuBean getNpcmenubean() {
		return npcmenubean;
	}

	public static void setNpcmenubean(NpcMenuBean npcmenubean) {
		ControlNpcXmlUntil.npcmenubean = npcmenubean;
	}

	public static String getType() {
		return type;
	}

	public static void setType(String type) {
		ControlNpcXmlUntil.type = type;
	}

	public static NpcSureMenuBean getSureBean() {
		return sureBean;
	}

	public static void setSureBean(NpcSureMenuBean sureBean) {
		ControlNpcXmlUntil.sureBean = sureBean;
	}

	public static String getNoteName1() {
		return noteName1;
	}

	public static void setNoteName1(String noteName1) {
		ControlNpcXmlUntil.noteName1 = noteName1;
	}

	public static String getSpeciesId() {
		return speciesId;
	}

	public static void setSpeciesId(String speciesId) {
		ControlNpcXmlUntil.speciesId = speciesId;
	}

	public static boolean isYesOrNo() {
		return yesOrNo;
	}

	public static void setYesOrNo(boolean yesOrNo) {
		ControlNpcXmlUntil.yesOrNo = yesOrNo;
	}

	public static RoleInitBean getRoleInitBean() {
		return roleInitBean;
	}

	public static void setRoleInitBean(RoleInitBean roleInitBean) {
		ControlNpcXmlUntil.roleInitBean = roleInitBean;
	}

	public static LoginResult getLoginResult() {
		return loginResult;
	}

	public static void setLoginResult(LoginResult loginResult) {
		ControlNpcXmlUntil.loginResult = loginResult;
	}

}
